package courseSystem.data;


import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DB {
    private static Connection conn = null;
    private static Statement st = null;
    private static ResultSet rs = null;
    private static Boolean flag = false;
    private static StringBuilder s;
    private void getConn() {
        try {
            conn = ((DataSource) (new InitialContext()).lookup("java:/comp/env/sc")).getConnection();
        } catch (Exception e) {
            new FO().logFile("连接异常:" + e);
        }
    }
    public List<HashMap<String,String>> query(String air, String table, String condition){
        List<HashMap<String,String>> list = new ArrayList<>();
        s= new StringBuilder("select " + air + " from " + table);
        if(!condition.isEmpty())
            s.append(" where ").append(condition);
        try {
            getConn();
            st=conn.createStatement();
            rs=st.executeQuery(String.valueOf(s));
            while (rs.next()){
                HashMap<String,String> map = new HashMap<>();
                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                    map.put(rs.getMetaData().getColumnName(i),rs.getString(i));
                }
                list.add(map);
            }
        }catch (Exception e){
            new FO().logFile("查询异常："+e.getMessage());
        }finally {
            new FO().logFile("查询语句："+s);
            close();
        }
        return list;
    }
    public boolean update(String table,String[] air,String[] value,String condition){
        s= new StringBuilder("update " + table + " set ");
        for (int i = 0; i < air.length; i++)
            s.append(air[i]).append("='").append(value[i]).append(i != air.length - 1 ? "'," : "'");
        if(!condition.isEmpty())
            s.append(" where ").append(condition);
        try {
            getConn();
            st=conn.createStatement();
            st.execute(String.valueOf(s));
            flag=st.getUpdateCount()>0;
        }catch (Exception e){
            flag=false;
            new FO().logFile("更新异常："+e.getMessage());
        }finally {
            new FO().logFile("更新语句："+s);
            close();
        }
        return flag;
    }
    public boolean insert(String table,String[] air,String[] value){
        s= new StringBuilder("insert into " + table + "(");
        for (int i = 0; i < air.length; i++)
            s.append(air[i]).append(i != air.length - 1 ? "," : ") values(");
        for(int i = 0; i < value.length; i++)
            s.append("'").append(value[i]).append(i != value.length - 1 ? "'," : "')");
        try {
            getConn();
            st=conn.createStatement();
            st.execute(String.valueOf(s));
            flag=st.getUpdateCount()>0;
        }catch (Exception e){
            flag=false;
            new FO().logFile("插入异常："+e.getMessage());
        }finally {
            new FO().logFile("插入语句："+s);
            close();
        }
        return flag;
    }
    public boolean delete(String table,String condition){
        s= new StringBuilder("delete from " + table);
        if(!condition.isEmpty())
            s.append(" where ").append(condition);
        try {
            getConn();
            st=conn.createStatement();
            st.execute(String.valueOf(s));
            flag=st.getUpdateCount()>0;
        }catch (Exception e){
            flag=false;
            new FO().logFile("删除异常："+e.getMessage());
        }finally {
            new FO().logFile("删除语句："+s);
            close();
        }
        return flag;
    }
    private void close() {
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
           new FO().logFile("关闭异常：" + e.getMessage());
        }
    }
}
